package cn.echogo.demo.activity;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import android.os.Bundle;
import cn.echogo.demo.model.CMD;
import cn.echogo.demo.model.UserBean;
import cn.echogo.demo.model.XID;
import cn.echogo.framework.activity.ExecutableActivity;
import cn.echogo.framework.http.EGHttpClientException;
import cn.echogo.framework.http.ResponseEntity;
import cn.echogo.framework.http.bak.RequestParams;
import cn.echogo.framework.model.PageBean;
import cn.echogo.ipart.R;

import com.google.gson.reflect.TypeToken;

public class HttpTestActivity extends ExecutableActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_http_test);
		
		Bundle bundle  = new Bundle();
		bundle.putString("id", "121");
		execute(XID.QUERY_USER, bundle);
		
	}

	@Override
	public String getUrl(String action) {
		return "http://mobile.chnlove.com/" + action;
	}

	@Override
	public Bundle onExecute(int id, Bundle bundle) throws Exception {
		Bundle result = null;
		switch (id) {
		case XID.QUERY_USER: {
			RequestParams params = new RequestParams("id", "121");
			params.put("pageIndex", String.valueOf(1));
			params.put("pageSize", String.valueOf(30));
			ResponseEntity rspe = application.getHttpClient().post(getUrl(CMD.USER_LIST), params);
			result = new Bundle();
			result.putSerializable("pageBean", rspe.getData("pageBean", PageBean.class));
			TypeToken<List<UserBean>> token = new TypeToken<List<UserBean>>() {
			};
			result.putSerializable("userList", (Serializable) rspe.getData(token.getType()));
		}
			break;
		}
		return result;
	}

	@Override
	public void onPostExecute(int id, Bundle bundle) {
		switch (id) {
		case XID.QUERY_USER: {
			// 显示视图
		}
			break;
		}
	}

	@Override
	public void onException(int id, Bundle p, Exception e) {
		if (e instanceof EGHttpClientException) {
			e.printStackTrace();
			Throwable cause = e.getCause();
			if (cause != null && cause instanceof IOException) {
				// 显示是否重试，重试，则再去execute id and p
			}
		}
	}

	@Override
	protected void onDestroy() {
		cancelExecute(XID.QUERY_USER);
		super.onDestroy();
	}

}
